package GridGames;
import java.io.IOException;
import java.util.Scanner;
public class ticTacToe extends Grid{
	Scanner input = new Scanner(System.in);
	public ticTacToe(){
		super(3,3);
		
	}
	
	public boolean checkWin(State state){
		if(
			(grid[0][0].state == state &&
			grid[0][1].state == state &&
			grid[0][2].state == state) ||
			
			(grid[1][0].state == state &&
			grid[1][1].state == state &&
			grid[1][2].state == state) ||
			
			(grid[2][0].state == state &&
			grid[2][1].state == state &&
			grid[2][2].state == state) ||
			//Checking horizontal
			(grid[0][0].state == state &&
			grid[1][0].state == state &&
			grid[2][0].state == state) ||
			
			(grid[0][1].state == state &&
			grid[1][1].state == state &&
			grid[2][1].state == state) ||
			
			(grid[0][2].state == state &&
			grid[1][2].state == state &&
			grid[2][2].state == state) ||
			//Checking Diagonals
			(grid[0][0].state == state &&
			grid[1][1].state == state &&
			grid[2][2].state == state) ||
			
			(grid[0][2].state == state &&
			grid[1][1].state == state &&
			grid[2][0].state == state)  
			
			){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean checkCatGame(){
		for(int x = 0; x < height; x++){
			for(int c = 0; c < width; c++){
				if(grid[c][x].state == State.NONE){
					return false;
				}
			}
		}
		
		return true;
	}
	
	private void arrayIndexWarning(){
		System.out.println("Please choose a number 1 through 3 for the X and Y coordinates.");
	}
	
	public void printHelp(){
		//Load help text in from help file! 
	}
	
	public void printCat(){
		System.out.println("CAT GAME!!");
	}
	
	public void placeToken(State state){
		String tokenName;
		int cellX;
		int cellY;
		if(state == State.TRUE){
			tokenName = "X";
		}else{
			tokenName = "O";
		}
		System.out.println("What tile do you want to place, player " + tokenName + "?");
		
		try{
		cellX = input.nextInt();
		cellY = input.nextInt();
		
		cellX--;
		cellY--;
		
		}catch(java.util.InputMismatchException e){
			System.out.println("Please input a valid number");
			System.out.println("'" + input.nextLine() + "'" + " Is not valid input");
			placeToken(state);
			return;
		}
			if(grid[cellX][cellY].state != State.NONE){
			System.out.println("That cell is already occupied, choose another.");
			placeToken(state);
			return;
		}
		
		if(cellX > 3 || cellY > 3){ //Could put an abstracted method like this in superclass.
			arrayIndexWarning();
			placeToken(state);
			return;
		}
		
		grid[cellX][cellY].state = state;
	}
	
	public void printWin(State state){
		if(state == State.TRUE){
			System.out.println("X WON!!");
		}else{
			System.out.println("O WON!!");
		}
	}
	
	@Override 
	public void printGrid(){
		for(int x = 0; x < height; x++){
			for(int c = 0; c < width; c++){
				String tokenName = " ";
				if(grid[c][x].state == State.TRUE){
					tokenName = "X";
				}else if(grid[c][x].state == State.FALSE){
					tokenName = "O";
				}else{
					tokenName = "&";
				}
				System.out.print(tokenName);
			}
			System.out.println();
		}
	}
	
	public void start(){
		printGrid();
		while(!checkWin(State.TRUE) && !checkWin(State.FALSE)){
			
			
			placeToken(State.TRUE);
			printGrid();
			
			if(checkWin(State.TRUE)){
				printWin(State.TRUE);
				break;
			}else if(checkCatGame()){
				printCat();
				break;
			}
			
			placeToken(State.FALSE);
			printGrid();
			
			if(checkWin(State.FALSE)){
				printWin(State.FALSE);
				break;
			}else if(checkCatGame()){
				printCat();
				break;
			}
		}
		
		System.out.println("Thanks for playing!");
	}
}
